package xna.android;

import br.com.R;
import android.app.Activity;
import android.content.Context;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.opengl.GLSurfaceView;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.content.res.AssetManager;

public class Game1 extends GLSurfaceView implements
		SurfaceHolder.Callback {
	
	private static final String TAG = Game1.class.getSimpleName();;
	
	private MainThread thread;
	
	private Sprite ball;
	private Sprite pad;
	
	Paint p = new Paint();
	//private SpriteAnimated megaMan;
	
	private AssetManager am;
	
	int brickCount = 0;
	private Sprite brick;
	
	int[][] level = {
			{0, 0, 0, 1, 1, 1, 0, 0, 0},
			{0, 0, 1, 1, 1, 1, 1, 0, 0},
			{0, 1, 1, 1, 1, 1, 1, 1, 0},
			{1, 1, 1, 1, 1, 1, 1, 1, 1},
			{1, 1, 1, 1, 0, 1, 1, 1, 1},
			{1, 1, 1, 0, 0, 0, 1, 1, 1},
			{1, 1, 1, 1, 0, 1, 1, 1, 1},
			{1, 1, 1, 1, 1, 1, 1, 1, 1},
			{0, 1, 1, 1, 1, 1, 1, 1, 0},
			{0, 0, 1, 1, 1, 1, 1, 0, 0},
			{0, 0, 0, 1, 1, 1, 0, 0, 0}
			
	};
	
	
	public Game1(Context Content)
	{
		super(Content);

		getHolder().addCallback(this);
		
		am = Content.getAssets();
		// a imagem:
		
		brick = new Sprite(BitmapFactory.decodeResource(getResources(),
				R.drawable.brick), 8, 4);
		
		ball = new Sprite(BitmapFactory.decodeResource(getResources(),
				R.drawable.bola), 50, 50);
		
		pad = new Sprite(BitmapFactory.decodeResource(getResources(),
				R.drawable.traco), 250, 450);
		
		//megaMan = new SpriteAnimated(BitmapFactory.decodeResource(getResources(), R.drawable.megamann), 20, 20, 35, 82, 24, 15);			
		// cria o loop principal
		// thread = new MainThread();
		thread = new MainThread(getHolder(), this);
		// passando o holder e o painel para a thread

		setFocusable(true);
	}

	// @Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width, int height)
	{}

	// @Override
	public void surfaceCreated(SurfaceHolder holder)
	{
		thread.setRunning(true);
		thread.start();
	}

	// @Override
	public void surfaceDestroyed(SurfaceHolder holder)
	{
		boolean retry = true;
		while (retry)
		{
			try
			{
				thread.join();
				retry = false;
			} catch (InterruptedException e) {
				// tenta de novo interromper
			}
		}
	}

	@Override
	public boolean onTouchEvent(MotionEvent event)
	{
		if (event.getAction() == MotionEvent.ACTION_DOWN)
		{
			// controle de evento com o sprite
			ball.handleActionDown((int) event.getX(), (int) event.getY());
			pad.handleActionDown((int) event.getX(), (int) event.getY());

			// checagem do toque embaixo da tela
			if (event.getY() > getHeight() - 10)
			{
				thread.setRunning(false);
				((Activity) getContext()).finish();
			} else {
				Log.d(TAG, "Coords: x=" + event.getX() + ",y=" + event.getY());
			}
		}
		if (event.getAction() == MotionEvent.ACTION_MOVE)
		{
			// gestos
			if (ball.isTouched()) {
				// tocado!
				ball.setX((int) event.getX());
				ball.setY((int) event.getY());
			}
			if (pad.isTouched()) {
				// tocado!
				pad.setX((int) event.getX());
				//sprite2.setY((int) event.getY());
			}
		}
		if (event.getAction() == MotionEvent.ACTION_UP)
		{
			// larguei
			if (ball.isTouched())
			{
				ball.setTouched(false);
			}
			if (pad.isTouched())
			{
				pad.setTouched(false);
			}
		}
		return true;
	}

	public boolean onKeyDown(int keyCode, KeyEvent ev)
	{ 
		switch (keyCode)
		{
			case KeyEvent.KEYCODE_A:
			
				Log.d("tecla", "A");
				
			break;
			case KeyEvent.KEYCODE_B:
			
				Log.d("tecla", "B");
			break;
		}
		return true;
	}
	
	// sobrecarregando o m�todo onDraw
		@Override
		protected void onDraw(Canvas spriteBatch)
		{
			// o bitmap factory deve ser importado
			// o resources drawable android � criado automaticamente com o nome da
			// imagem
			// o 10 10 � a posicao
			// o null � a mascara
			// canvas.drawBitmap(BitmapFactory.decodeResource(getResources(),
			// R.drawable.android), 10, 10, null);

			// preenchimento
			spriteBatch.drawColor(Color.BLACK);
			ball.draw(spriteBatch);
			pad.draw(spriteBatch);
	        //seta a cor e a fonte do texto		

			Typeface plain = Typeface.createFromAsset(am, "fonts/comic.ttf");
			Typeface tp = Typeface.create(plain, Typeface.NORMAL);
					
			p.setColor(Color.RED);
			p.setTypeface(tp);
			

			for(int h = 0; h < level.length; h++)
			{
				for(int w = 0; w < 9; w++)
				{
					if(level[h][w] == 1)
					{
						brick.setX(w * 34);
						brick.setY(h * 17);
						brick.draw(spriteBatch);	
					}
				}
			}

			//megaMan.draw(spriteBatch);	
		}

	public void Update() 
	{	
		ball.update(getWidth(), getHeight());
		pad.KeepOnScreen(getWidth(), getHeight());

		if (ball.intersects(pad.getX(), pad.getY(), pad.getBitmap().getWidth(), pad.getBitmap().getHeight()) && ball.isVisible())
		{
			ball.ToggleYDirection();
			ball.AddSpeed((2/3), (2/3));
		}
		
		/*if (ball.intersects(brick.getX(), brick.getY(), brick.getBitmap().getWidth(), brick.getBitmap().getHeight()) && ball.isVisible())
		{
			ball.ToggleYDirection();
			ball.AddSpeed((2/3), (2/3));
			brick.setVisible(false);
		}*/

		//megaMan.update(System.currentTimeMillis()); 
	}
	
	public void Draw(Canvas canvas)
	{
		onDraw(canvas);
	}
}